#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import threading
import time

rlock = threading.RLock()
lk1 = rlock
lk2 = rlock

class Mythread(threading.Thread):
    def run(self):
        self.foo()
        self.bar()

    def foo(self):
        lk1.acquire()
        print("取到第一把锁， {0}".format(threading.current_thread().getName()))
        lk2.acquire()
        print("取到第二把锁， {0}".format(threading.current_thread().getName()))
        lk2.release()
        lk1.release()

    def bar(self):
        lk2.acquire()
        print("B第一把锁 {}".format(self.name))
        lk1.acquire()
        time.sleep(1)
        print("B第二把锁 {}".format(self.name))
        lk1.release()
        lk2.release()

if __name__ == '__main__':
    t1 = Mythread()
    t1.start()

# def myThread():
#     number = 0
#     rlock.acquire()   # 先获取锁
#     for i in range(1000000):
#         number += i
#     print("线程名: {0} 结果: {1}".format(threading.current_thread().getName(), number))
#     rlock.release()
#
# if __name__ == '__main__':
#     for i in range(2):
#         t = threading.Thread(target=myThread)
#         t.start()
#     print("主线程结束")